package model;



import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

// 博客表的相管数据库操作
public class ArticleDao {
    // C R U D
    public boolean add(Article article){
        Connection connection = DBUtil.getConnection();
        String sql = "insert into article values (null,?,?,?)";
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setString(1,article.getTitle());
            statement.setString(2,article.getContent());
            statement.setInt(3,article.getUserId());
            int ret = statement.executeUpdate();
            if(ret != 1){
                return false;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            DBUtil.close(connection,statement,null);
        }
        return true;
    }

    // 查看所有文章
    public List<Article> selectAll(){
        List<Article> articles = new ArrayList<>();
        Connection connection = DBUtil.getConnection();
        // 拼装sql
        String sql = "select articleId,title,userId from article";
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {

            statement =  connection.prepareStatement(sql);
            // 执行sql
            resultSet = statement.executeQuery();
            // 遍历结果集
            while(resultSet.next()){
                Article article = new Article();
                article.setArticleId(resultSet.getInt("articleId"));
                article.setTitle(resultSet.getString("title"));
                article.setUserId(resultSet.getInt("userId"));
                articles.add(article);
            }
            return articles;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }

    // 查看文章详情;
    public Article selectById(int articleId){
        Connection connection = DBUtil.getConnection();

        String sql = "select * from article where articleId = ?";
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            statement =  connection.prepareStatement(sql);
            statement.setInt(1,articleId);

            resultSet = statement.executeQuery();

            if(resultSet.next()){
                Article article = new Article();
                article.setUserId(resultSet.getInt("userId"));
                article.setTitle(resultSet.getString("title"));
                article.setArticleId(resultSet.getInt("articleId"));
                article.setContent(resultSet.getString("content"));
                return article;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }

    //删除 指定 文章
    public boolean delete(int articleId){
        Connection connection = DBUtil.getConnection();
        String sql = "delete from article where articleId = ?";
        PreparedStatement statement = null;

        try {
            statement = (PreparedStatement) connection.prepareStatement(sql);
            statement.setInt(1,articleId);
            int ret = statement.executeUpdate();
            if(ret != 1){
                return false;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }
        return true;
    }
}
